angular.module('${menuCode}')
.controller("${widgetCode}Ctrl",function($scope,AppKit){
	$scope.closeModal = function() {
		AppKit.closeModal();
	};
	
	$scope.loadTransportationSelect = function(){
		var url='/aeaiem/services/DailyReiDataProvider/rest/get-select-list/TRANSPORTATION_WAY';
		var promise = AppKit.getJsonApi(url);  
		promise.success(function(rspJson){
			$scope.transportationSelect= rspJson;
			
		})
	}
	
	$scope.loadTransportationSelect();
	
	$scope.loadProjectSelect = function(){
		var url='/aeaiem/services/EvectionReiDataProvider/rest/get-project-select';
		var promise = AppKit.getJsonApi(url);  
		promise.success(function(rspJson){
			$scope.projectSelect= rspJson;
			
		})
	}
	
	$scope.loadEvectionListData = function() {
		var url='/aeaiem/services/EvectionReiDataProvider/rest/evection-rei-Detail/'+$scope.detailId;
		var promise = AppKit.getJsonApi(url);  
		promise.success(function(rspJson){
			$scope.evectionData = rspJson;
			
			$scope.evectionData.WFP_ID = $scope.WFP_ID;
			$scope.evectionData.WFA_CODE = $scope.WFA_CODE;
			$scope.evectionData.WFIP_ID = $scope.WFIP_ID;
			
			$scope.buttonShowState = true;
			
			$scope.evectionData.commitTime = new Date($scope.evectionData.commitTime);
			$scope.evectionData.startTime = new Date($scope.evectionData.startTime);
			$scope.evectionData.endTime = new Date($scope.evectionData.endTime);
			
			
			for(var i = 0; i < $scope.evectionData.expenseList.length;i++){
				$scope.evectionData.expenseList[i].sdate = new Date($scope.evectionData.expenseList[i].sdate);
				$scope.evectionData.expenseList[i].edate = new Date($scope.evectionData.expenseList[i].edate);
			}
			
			
			
		})
	}
	
		$scope.loadEvectionListData();
		
	$scope.exSubmitAgainDetailInfo = function() {
		var url ='/aeaiem/services/TodoTaskDataProvider/rest/expenses-todo-task-again';
		$scope.evectionData.operationType  = $scope.operationType;
		for(var i = 0; i < $scope.evectionData.expenseList.length;i++){
			$scope.evectionData.expenseList[i].traSelect = "";
			$scope.evectionData.expenseList[i] =  JSON.stringify($scope.evectionData.expenseList[i]);
		}
		
		AppKit.postJsonApi(url,$scope.evectionData).then(function(rspJson){
			AppKit.successPopup({"title":"提交成功!"});
			$scope.loadToduTaskDate();
			AppKit.closeModal();
			
			AppKit.hideMask();
		}); 
	}
	
	$scope.exCancelAgainDetailInfo = function() {
		var url ='/aeaiem/services/TodoTaskDataProvider/rest/expenses-todo-task-cancel';
		$scope.evectionData.operationType  = $scope.operationType;
		for(var i = 0; i < $scope.evectionData.expenseList.length;i++){
			$scope.evectionData.expenseList[i].traSelect = "";
			$scope.evectionData.expenseList[i] =  JSON.stringify($scope.evectionData.expenseList[i]);
		}
		
		AppKit.postJsonApi(url,$scope.evectionData).then(function(rspJson){
			AppKit.successPopup({"title":"废弃成功!"});
			$scope.loadToduTaskDate();
			AppKit.closeModal();
			
			AppKit.hideMask();
		}); 
	}
	
	
	$scope.addExpenseList = function() {
		$scope.datasLength = $scope.evectionData.expenseList.length;
		$scope.evectionData.expenseList[$scope.datasLength] = new expenseListData();
	}
	
	$scope.delData=function(index){
		$scope.evectionData.expenseList.splice(index,1);
	    $scope.datasLength = $scope.evectionData.expenseList.length;
	}
	
	 $scope.delData=function(index,id){
	    	var confirmPopup = $ionicPopup.confirm({
		           title: '确认删除吗?',
		           cancelText:'取消',
		           cssClass:'confirmBox'
		      });

	    	 confirmPopup.then(function(res) {
	    		 if(res){
		    		 $scope.evectionData.expenseList.splice(index,1);
		    	     $scope.datasLength = $scope.evectionData.expenseList.length;
		    	     if(id){
		    	    	var url ='/aeaiem/services/EvectionReiDataProvider/rest/del-evection-list/'+id+'/'+ $scope.evectionData.id;
		    	 		var promise = AppKit.getJsonApi(url);  
		    	 		promise.success(function(rspJson){
		    	 			AppKit.hideMask();
		    	 		});
		    	     }
	    		 }
	    	 });
		}
	
	$scope.sum = function(){
		var list = $scope.evectionData.expenseList;
		var j = 0;
		for(var i=0;i<list.length;i++){
			if(list[i].transportationFee){
				j += list[i].transportationFee
			}
			if(list[i].horelFee){
				j += list[i].horelFee
			}
			if(list[i].otherFee){
				j += list[i].otherFee
			}
		}
		$scope.evectionData.money = j+$scope.evectionData.subsidy;
	}
	
	function expenseData(){ 
	    this.id = "";
		this.projectId = "";
		this.togetherPerson = "";
		this.startTime = "";
		this.endTime = "";
		this.subsidy = "";
		this.desc = "";
		this.money = "";
		this.evectionDay = "";
		this.commitType = "UNSUBMITTED";
		this.expenseList = [];
    }

	function expenseListData(){ 
		this.expId = "";   
		this.departure = "";
		this.destination = "";
		this.sdate = "";
		this.edate = "";
		this.transportation = "";
		this.transportationFee = "";
		this.horelFee = "";
		this.otherFee = "";
		this.remarks = "";
		this.traSelect = $scope.transportationSelect;
	 }
	
	
});